'''
    Goblinoid: Experience all of MediaGoblin on an Android Device
    Copyright (C) 2015 Dylan Jeffers

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

# models for app root
from __future__ import print_function
from os.path import join

from kivy.app import App
from kivy.properties import ObjectProperty, DictProperty, \
    NumericProperty, BooleanProperty
from kivy.uix.widget import Widget

from pypump import JSONStore

# TODO come up with a better name than feedsections + modularize it more
import goblinoid.config.feedsections as fs
from goblinoid.services.profilemanager import ProfileManager
from goblinoid.services.activitymanager import ActivityManager
from goblinoid.services.feedservice import FeedService


# store for user's credentials
class UserStore(JSONStore):
    @classmethod
    def get_filename(cls):
        return join(App.get_running_app().user_dir,
                    'credentials.json')


class PumpService(Widget):

    # TODO: move many of these properties into other classes
    pump = ObjectProperty(None)
    comment_modals = DictProperty()

    activity_manager = ObjectProperty()
    profile_manager = ObjectProperty()
    feed_service = ObjectProperty()

    def __init__(self, activity_dict_style=fs.mg_activity_dict):
        super(PumpService, self).__init__()
        self.profile_manager = ProfileManager()
        self.activity_manager = ActivityManager()
        self.data_dir = join(App.get_running_app().user_dir)

    @classmethod
    def pumpity(cls):
        return cls(fs.pumpity_activity_dict)

    def get_data_dir(self):
        return self.data_dir

    def on_pump(self, instance, pump):
        self.feed_service = FeedService(
            pump=self.pump,
            pump_service=self,
            profile_manager=self.profile_manager,
            activity_manager=self.activity_manager)
        self.profile_manager.add_profile(pump.me)

    def get_profile_service(self, webfinger):
        return self.profile_manager.get_profile_service(webfinger)

    def add_profiles_from_activity(self, activity):
        self.profile_manager.add_profile(activity.actor)
        for comment in activity.obj.comments:
            self.profile_manager.add_profile(comment.author)
